The C++ Workshop by Dale Green Kurt Guntheroth and Shaun Ross Mitchell

The C++ Workshop by Dale Green Kurt Guntheroth and Shaun Ross Mitchell

Author:Dale Green, Kurt Guntheroth, and Shaun Ross Mitchell
Language: eng
Format: mobi
Publisher: Packt Publishing Pvt Ltd
Published: 2020-02-04T16:00:00+00:00


The next step is to begin entering the body of main(). First, declare an instance of numeric_list called l. This function-local variable will be destroyed when main() returns: numeric_list l;

Create a for loop to add five items to the list, and then print out the list: for (int i = 1; i < 6; ++i)

{

l.add(i);

}

l.print();

Declare a numeric_item pointer called p, and assign to p the value returned by l.find(4). The value in p is an unowned pointer. We already know find() will discover an item with this value because we added it moments ago. Output a message if the returned pointer is not nullptr, just to be sure: numeric_item* p = l.find(4);

if (p != nullptr)

cout << "found numeric_item 4" << endl;

That's it. When main() returns, p is still pointing to an item in l. p isn't deleted, but that's fine because p is an unowned pointer.

As main() returns, l's destructor is called. Since l has an owned pointer, l's destructor must delete anything it points to, which is the entire list.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.